package recommend;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Map.Entry;
import java.util.SortedMap;
import java.util.TreeMap;

import org.ejml.ops.CommonOps;
import org.ejml.simple.SimpleMatrix;
import org.ejml.simple.SimpleSVD;


public class LectureAdvisor {
	
	private double[][] data;
	
	public LectureAdvisor(double[][] data) {
		this.data = data;
	}
	
	private SimpleMatrix calculateReducedDimMatrix(){
		SimpleMatrix dataMatrix = new SimpleMatrix(data);
		SimpleSVD<SimpleMatrix> svdResult = dataMatrix.svd();
		SimpleMatrix w = svdResult.getW().extractMatrix(0, 1, 0, 1);
		SimpleMatrix v = svdResult.getV();
		v = v.extractMatrix(0, v.numRows(), 0, 1).transpose();
		SimpleMatrix reducedDim = w.mult(v);
		return reducedDim;
	}
	
	
	
	public ClusterInfo extractLectureClusters(){
		double[][] data = RecommendationCalculator.getData();
//		SimpleMatrix reducedDimMatrix = calculateReducedDimMatrix();
//		SortedMap<Double, Integer> sorted = new TreeMap<Double,Integer>();
//		for (int i = 0; i < reducedDimMatrix.getNumElements(); i++) {
//			sorted.put(reducedDimMatrix.get(i), i);
//		}
//		ClusterInfo cInfo = new ClusterInfo();
//		int clusterCounter = 0;
//		Iterator<Entry<Double,Integer>> it = sorted.entrySet().iterator();
//		Entry<Double,Integer> previousEntry = null;
//		if(it.hasNext()){
//			previousEntry = it.next();
//			List<Integer> currentCluster = new ArrayList<Integer>();
//			currentCluster.add(previousEntry.getValue());
//			cInfo.getClusterIdxAndRelatedSeqIds().put(clusterCounter, currentCluster);
//		}
//		Entry<Double,Integer> nextEntry=null;
//		while(it.hasNext()){
//			Entry<Double,Integer> currentEntry = it.next();
//			if(it.hasNext()){
//				nextEntry = it.next();
//				if(currentEntry.getKey() - previousEntry.getKey() > nextEntry.getKey() - currentEntry.getKey()){
//					cInfo.getClusterIdxAndRelatedSeqIds().put(clusterCounter, new ArrayList<Integer>());
//				}
//			}else{
//				++clusterCounter;
//				cInfo.getClusterIdxAndRelatedSeqIds().put(clusterCounter, new ArrayList<Integer>());
//			}
//			cInfo.getClusterIdxAndRelatedSeqIds().get(clusterCounter).add(currentEntry.getValue());
//			previousEntry = currentEntry;
//		}
//		if(nextEntry!= null){
//			cInfo.getClusterIdxAndRelatedSeqIds().get(clusterCounter).add(nextEntry.getValue());
//		}
//		for (Integer cls : cInfo.getClusterIdxAndRelatedSeqIds().keySet()) {
//			for (Integer seq : cInfo.getClusterIdxAndRelatedSeqIds().get(cls)) {
//				cInfo.getSequenceAndClusterIdxMap().put(seq, cls);
//			}
//		}
		
		return null;
			
	}
	
	
	
}
